package com.yuxi.Controller.Impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.yuxi.Config.ConfigStatic;
import com.yuxi.Controller.InformationSubController;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Response.Msg;
import com.yuxi.Service.UserInformationService;
import com.yuxi.Service.UserService;
import com.yuxi.Service.UserSubService;
import com.yuxi.Util.IPUtil;

import jdk.nashorn.internal.runtime.regexp.joni.Config;

@Controller
public class InformationSubControllerImpl implements InformationSubController {
	@Autowired
	private UserSubService userSubService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserInformationService userInformationService;
	
	private Gson gson=new Gson();
	@Override
	public String SubToFirstCheck(int infoId, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.subToFirstCheck(infoId, userService.getCurLoginUser(session), ip);
			if(flag){
				Msg msg=new Msg(true, "提交成功", System.currentTimeMillis(), null, "提交信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1001", "提交信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1002", "提交信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String SubToReCheck(int infoId,String remark,String operation, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.subToReCheck(infoId, userService.getCurLoginUser(session), ip,remark,operation);
			if(flag){
				Msg msg=new Msg(true, "提交成功", System.currentTimeMillis(), null, "提交信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1001", "提交信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1002", "提交信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String SubToCheck(int infoId,String remark,String operation, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.subToCheck(infoId, userService.getCurLoginUser(session), ip,remark,operation);
			if(flag){
				Msg msg=new Msg(true, "提交成功", System.currentTimeMillis(), null, "提交信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1001", "提交信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1002", "提交信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String SubToInfoResearch(int infoId,String remark,String operation, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.subToInfoResearch(infoId, userService.getCurLoginUser(session), ip,remark,operation);
			if(flag){
				Msg msg=new Msg(true, "提交成功", System.currentTimeMillis(), null, "提交信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1001", "提交信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1002", "提交信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String SubToTaskDispatch(int infoId, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.subToTaskDispatch(infoId, userService.getCurLoginUser(session), ip);
			if(flag){
				Msg msg=new Msg(true, "提交成功", System.currentTimeMillis(), null, "提交信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1001", "提交信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1002", "提交信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String SubToCurTaskDispatch(int infoId,String remark, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.subToCurTaskDispatch(infoId, userService.getCurLoginUser(session), ip,remark);
			if(flag){
				Msg msg=new Msg(true, "提交成功", System.currentTimeMillis(), null, "提交信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1001", "提交信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1002", "提交信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String taskSubToTaskFinish(int infoId, HttpSession session, HttpServletRequest request) {
		return SubToTaskFinish(ConfigStatic.TASKTYPETABLE.get("Task"), infoId, session, request);
	}
	
	private  String SubToTaskFinish(int taskType,int infoId, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.subToTaskFinsh(infoId, userService.getCurLoginUser(session), ip,taskType);
			if(flag){
				Msg msg=new Msg(true, "提交成功", System.currentTimeMillis(), null, "提交信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1001", "提交信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "提交失败", System.currentTimeMillis(), "1002", "提交信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String curTaskSubToTaskFinish(int infoId, HttpSession session, HttpServletRequest request) {
		return SubToTaskFinish(ConfigStatic.TASKTYPETABLE.get("CurTask"), infoId, session, request);
	}

	@Override
	public String ReToInfoMannger(int infoId,String remark,String operation, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			Boolean flag=userSubService.ReToInfoMannger(infoId, userService.getCurLoginUser(session), ip,remark,operation);
			if(flag){
				Msg msg=new Msg(true, "退回成功", System.currentTimeMillis(), null, "退回信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "退回失败", System.currentTimeMillis(), "1001", "退回信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "退回失败", System.currentTimeMillis(), "1002", "退回信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String dispatchInfoToAny(Integer infoId, Integer flowId, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			UserInfo u=userService.getCurLoginUser(session);
			Boolean flag=userSubService.dispatchInfoToAny(infoId, flowId, u, ip);
			if(flag){
				Msg msg=new Msg(true, "调配成功", System.currentTimeMillis(), null, "调配信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "调配失败", System.currentTimeMillis(), "1001", "调配信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "调配失败", System.currentTimeMillis(), "1002", "调配信息");
			return gson.toJson(msg);
		}
	}
	@Override
	public String subToInfoFinsh(Integer infoId,String remark,HttpSession sesion, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			UserInfo u=userService.getCurLoginUser(sesion);
			Boolean flag=userSubService.subToInfoFinish(infoId, u, ip,remark);
			if(flag){
				Msg msg=new Msg(true, "结束成功", System.currentTimeMillis(), null, "结束信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "结束失败", System.currentTimeMillis(), "1001", "结束信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "结束失败", System.currentTimeMillis(), "1002", "结束信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String reCheckToFirstCheck(Integer infoId,String remark,String operation, HttpSession session, HttpServletRequest request) {
		try{
			String ip=IPUtil.getIpAddr(request);
			UserInfo u=userService.getCurLoginUser(session);
			Boolean flag=userSubService.reCheckToFirstCheck(infoId, remark, u, ip,operation);
			if(flag){
				Msg msg=new Msg(true, "退回成功", System.currentTimeMillis(), null, "退回信息");
				return gson.toJson(msg);			
			}else{
				Msg msg=new Msg(false, "退回失败", System.currentTimeMillis(), "1001", "退回信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "退回失败", System.currentTimeMillis(), "1002", "退回信息");
			return gson.toJson(msg);
		}
	}

	@Override
	public String delInfoById(Integer id) {
		try{
			userInformationService.delInfoById(id);
			Msg msg=new Msg(true, "删除成功", System.currentTimeMillis(), null, "删除信息");
			return gson.toJson(msg);
		}catch(Exception e){
			Msg msg=new Msg(false, "删除失败", System.currentTimeMillis(), "1002", "删除信息");
			return gson.toJson(msg);
		}
		
	}

	@Override
	public String delOwnExporId(Integer id) {
		try{
			userInformationService.delOwnExporId(id);
			Msg msg=new Msg(true, "删除成功", System.currentTimeMillis(), null, "删除信息");
			return gson.toJson(msg);
		}catch(Exception e){
			Msg msg=new Msg(false, "删除失败", System.currentTimeMillis(), "1002", "删除信息");
			return gson.toJson(msg);
		}
	}

}
